﻿using log4net;
using Mate.Caching;
using Mate.Caching.MemoryCache;
using Mate.Caching.Redis;
using Mate.Comment.Service;
using Mate.Core.Configuration;
using Mate.Db.SqlSugar;
using Mate.Framework.Engine;
using Mate.Good.Service;
using Mate.Merchant.Service;
using Mate.Other.Service;
using Mate.System.Service;
using Mate.User.Service;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using StructureMap;
using System;
using System.Collections.Generic;
using System.Text;

namespace Mate.Framework.Extensions
{
    public static class DependencyRegistrarExtensions
    {
        public static IServiceProvider Register(this IServiceCollection services, IConfiguration configuration, MateConfig mateConfig)
        {
            services.AddSingleton(x => LogManager.GetLogger(configuration.GetValue<string>("Log4net:Repository"), configuration.GetValue<string>("Log4net:Name")));
        
            services.AddScoped<DataClient>();

            if (mateConfig.RedisCachingEnabled)
            {
                services.AddSingleton<IRedisConnectionWrapper, RedisConnectionWrapper>();
                services.AddScoped<ICacheManager, RedisCacheManager>();
            }
            else
            {
                services.AddScoped<ICacheManager, MemoryCacheManager>();
            }
            services.AddScoped<IAdminUserService, AdminUserService>();//
            services.AddScoped<IRolesService, RolesService>();
            services.AddScoped<IMenuService, MenuService>();
            services.AddScoped<IMerchantCategoryService, MerchantCategoryService>();
            services.AddScoped<IDistrictService, DistrictService>();
            services.AddScoped<IMerchantTagService, MerchantTagService>();
            services.AddScoped<IMerchantService, MerchantService>();
            services.AddScoped<IMerchantOpentimeService, MerchantOpentimeService>();
            services.AddScoped<ICommentService, CommentService>();
            services.AddScoped<IGoodCategoryService, GoodCategoryService>();
            services.AddScoped<IReplyService, ReplyService>();
            services.AddScoped<IGoodService, GoodService>();
            services.AddScoped<IMpUserService, MpUserService>();
            services.AddScoped<IBannerService, BannerService>();
            services.AddScoped<IStatisticsDataService, StatisticsDataService>(); 
            services.AddScoped<IAppHomeMenuService, AppHomeMenuService>();
            services.AddScoped<ILikeService, LikeService>();


            var container = new Container();
            container.Populate(services);
            EngineContainerFactory.InitializeEngineContainerFactory(new EngineContainer(container));
            return container.GetInstance<IServiceProvider>();
        }
    }
}
